<?php

namespace app\api\controller;
use app\api\model\xykeep\User as xykeepUser;
use app\common\controller\Api;

use app\common\model\DepositOrder as DepositOrderModel;
use app\common\model\Teacher as TeacherModel;
use app\common\model\TeacherAudit;
use app\common\model\School as SchoolModel;
use app\common\validate\TeacherAudit as TeacherAuditValidate;
use app\common\validate\Comment as CommentValidate;
use app\common\model\TeacherChoose;
use app\common\validate\TeacherChoose as TeacherChooseValidate;
use app\common\model\SettledOrder as SettledOrderModel;
use app\common\model\UserCoupons;
use app\common\model\UserCouponsLog;
use app\common\model\Grade;
use app\common\model\Subject;
use app\common\model\TeacherOrder;
use app\common\model\TeacherCollect;
use app\common\model\SettledOrder;
use app\common\model\Comment;
use app\common\model\User as UserModel;
use think\Config as ConfigModel;
use think\Config as ThinkConfig;
use think\Db;
use think\Exception;
use think\Log;

class Teacher extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //找家教
    public function lists()
    {
        $param = $this->request->post();

        $data = TeacherModel::getLists($param);
        $this->success('success', $data);
    }

    public function detail(){
        $param = $this->request->post();

        $data = TeacherModel::where([
            'deletetime' => Db::raw('deletetime is null'),
            'user_id'=>$param['id']
        ])->find();
        if(!$data){
            $this->error('该教师已被注销');
        }
        $data['give_time'] = json_decode($data['give_time'],true);
        $data = TeacherModel::oneRowExtend($data);
        $data['is_collect'] = (bool)TeacherCollect::where([
            'user_id' => $this->auth->id,
            'teacher_id' => $param['id'],
        ])->find();
        $data['settled_all'] = $data['with']['settled_type'].','.$data['with']['settled_type_free'];
        $data['settled_all'] = explode(',', $data['settled_all']);
        $data['settled_all'] = array_filter($data['settled_all']);
        $data['settled_all'] = implode('/', $data['settled_all']);

        $give_lessons_text = $data['with']['give_lessons_text'];
        foreach ($give_lessons_text as $k=>&$v){
            $age_range_id = Db::name('grade')->where([
                'id' => $v['grade']
            ])->value('age_range_id');

            $age_range = Db::name('age_range')->where([
                'id' => $age_range_id
            ])->find();

            $v['age_range'] = $age_range['name'];

            $v['advise_settled'] = Db::name('age_range')->where([
                'id' => $age_range['id']
            ])->value('advise_settled');

        }

        $data['give_lessons_text'] = $give_lessons_text;


        if($data['with']['holiday_areas']){
            $data['with']['holiday_areas'] = str_replace(',','/',$data['with']['holiday_areas']);
        }
        if($data['with']['unholiday_areas']){
            $data['with']['unholiday_areas'] = str_replace(',','/',$data['with']['unholiday_areas']);
        }

        if(strstr($data['settled_all'],'专职教学家教')!==false){
            $data['only_teacher'] = 1;
        }else{
            $data['only_teacher'] = 0;
        }

        $this->success('success', $data);
    }

    //提交教师评价
    public function comment()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $param['user_id'] = $user_id;

        $validate = new CommentValidate();
        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }

        Db::startTrans();
        try{
            $data = TeacherOrder::where(['id'=>$param['order_id']])->find();
            if(!$data || $data['status'] !== 2 || $data['user_id']!==$user_id){
                throw new \Exception("订单不存在");

            }
            $commentModel = new Comment();


            $comment_insert = [
                'user_id' => $user_id,
                'teacher_id' => $param['teacher_id'],
                'order_id' => $param['order_id'],
                'star' => $param['star'],
                'type' => 'teacher',
                'content' => $param['content']
            ];
            //两个满评升星
            if($param['star']==5){
                $before_five_star = $commentModel->where([
                    'user_id' => $param['teacher_id'],
                    'star' => 5,
                    'star_clear' => 0
                ])->find();
                if($before_five_star){
                    $before_five_star->star_clear = 1;
                    $before_five_star->save();
                    $comment_insert['star_clear'] = 1;

                    $level_param['star'] = ConfigModel::get('site.comment_star');
                    $level_param['user_id'] = $param['teacher_id'];
                    $level_param['type'] = '两个满评升星';
                    \think\Hook::listen('teacher_star_change', $level_param);
                }
            }

            $commentModel->save($comment_insert);

            $data->status = 3;
            $data->save();

            $depositOrderModel = new DepositOrderModel();
            $deposit = $depositOrderModel->where([
                'teacher_order_id' => $data->id,
                'status' => 1,
                'service_type' => 'deposit'
            ])->find();

            if($deposit){
                $params['order'] = $deposit;
                $params['type'] = 'deposit';

                \think\Hook::listen('xykeep_order_refund_after', $params);
            }
            //教师试课奖励
            //            xykeepUser::money(ConfigModel::get('site.teacher_rewards'),$data['teacher_id'],'teacher_rewards','试课奖励', $data['id']);


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('评论成功');
    }

    public function comment_list(){
        $param = $this->request->post();
        $commentModel = new Comment();
        $data = $commentModel->where([
            'teacher_id' => $param['teacher_id'],
            'type' => 'teacher'
        ])->select();
        $this->success('评论列表',$data);
    }

    //保存草稿
    public function save_draft()
    {
        $param = $this->request->param();

        $param['user_id'] = $this->auth->id;

        $auditing = (new TeacherAudit())->where([
            'user_id' => $param['user_id'],
            'status' => 1
        ])->find();
        if($auditing){
            $this->error('您之前提交的资料正在审核，请直接选择修改资料');
        }

        try{
            $draft_data = (new TeacherAudit)->where([
                'user_id' => $param['user_id'],
                'status' => 0
            ])->find();

            $isUpdate = false;
            if($draft_data){
                $param['id'] = $draft_data['id'];
                $isUpdate = true;
            }

            $give_lessons = $param['give_lessons'];
            $_give_lessons = [];
            foreach ($give_lessons as $k=>$v){
                $_give_lessons[] = $v;
            }
            $param['give_lessons'] = json_encode($_give_lessons,true);

            $param['give_time'] = json_encode($param['give_time'],true);

            if($param['holiday_area']) {
                $param['holiday_area'] = implode(',', $param['holiday_area']);
            }

            if($param['holiday_areas']) {
                $param['holiday_areas'] = implode(',', $param['holiday_areas']);
            }

            if($param['unholiday_area']) {
                $param['unholiday_area'] = implode(',', $param['unholiday_area']);
            }

            if($param['unholiday_areas']) {
                $param['unholiday_areas'] = implode(',', $param['unholiday_areas']);
            }

            $param['photo_images'] = implode(',',$param['photo_images']);
            $param['tags'] = implode(',',array_filter($param['tags']));

            $teacher_exp = $param['teacher_exp'];
            $_teacher_exp = [];
            foreach ($teacher_exp as $k=>$v){
                if($v['start'] && $v['end'] && $v['content']){
                    $_teacher_exp[] = $v;
                }
            }
            $param['teacher_exp'] = json_encode($_teacher_exp,true);

            $param['honor_images'] = implode(',',$param['honor_images']);

            (new TeacherAudit)->isUpdate($isUpdate)->save($param);

        }catch (\Exception $e){
            Log::record($this->request->param(),'save_draft_orginal_log');
            Log::record($param,'save_draft_save_log');
            Log::record($e->getMessage(),'save_draft_save_msg');

            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('保存成功');

    }

    //申请入驻
    public function apply()
    {
        $param = $this->request->param();

        $param['user_id'] = $this->auth->id;
        $param['nickname'] = str_replace('老师','',$param['nickname']);
        $param['nickname'] = str_replace('教师','',$param['nickname']);

        $param['realname'] = str_replace('老师','',$param['realname']);
        $param['realname'] = str_replace('教师','',$param['realname']);

        if($param['nickname']){
            $nickname_find = UserModel::where([
                'id' => ['neq',$this->auth->id],
                'nickname' => $param['nickname'],
            ])->find();
            if($nickname_find){
                $this->error('该昵称已经被其他用户使用');
            }
            $mobile_find = UserModel::where([
                'id' => ['neq',$this->auth->id],
                'mobile' => $param['mobile'],
            ])->find();
            if($mobile_find){
                $this->error('该手机号已经被其他用户使用');
            }
        }

        try{
            $validate = new TeacherAuditValidate();

            if (!$validate->scene('add')->check($param)) {
                throw new Exception($validate->getError());
            }

            if(strstr($param['settled_type_free'],'专职教学家教')!==false){
                if(!$param['main_image']){
                    throw new Exception('请上传教师资格证！');
                }
            }
            if(strstr($param['settled_type'],'学校找代课教员')!==false){
                if(!$param['main_image']){
                    throw new Exception('请上传教师资格证！');
                }
            }
            if(strstr($param['settled_type'],'机构找代课教员')!==false){
                if(!$param['main_image']){
                    throw new Exception('请上传教师资格证！');
                }
            }


            Db::startTrans();
            $unpassData = (new TeacherAudit)->where([
                'user_id' => $param['user_id'],
                'status' => ['in','0,1']
            ])->find();

            //如果之前有保存草稿，或者未通过审核的，就为更新
            $isUpdate = false;
            if($unpassData){
                $param['id'] = $unpassData['id'];
                $isUpdate = true;
            }

            //如果之前有通过审核的，就为补充材料
            $passData = (new TeacherAudit)->where([
                'user_id' => $param['user_id'],
                'status' => 2
            ])->order('id desc')->find();
            if($passData){
                $param['is_fill'] = 1;
            }

            //付费入驻订单金额计算
            $settled_type_single = ThinkConfig::get('site.settled_price');
            $settled_type_count = 0;
            if(!($param['settled_type']==='')){
                $settled_type_count = count(explode(',',$param['settled_type']));
            }
            $already_settled = SettledOrderModel::where([
                'user_id' => $this->auth->id,
                'status' => 1
            ])->sum('nums');
            if($already_settled>=$settled_type_count){
                $money = 0;
            }else{
                $money = ($settled_type_count-$already_settled) * $settled_type_single;
            }

            $give_lessons = $param['give_lessons'];
            $_give_lessons = [];
            foreach ($give_lessons as $k=>$v){
                $_give_lessons[] = $v;
            }
            $param['give_lessons'] = json_encode($_give_lessons,true);

            $param['give_time'] = json_encode($param['give_time'],true);
            if($param['holiday_area']) {
                $param['holiday_area'] = implode(',', $param['holiday_area']);
            }

            if($param['holiday_areas']) {
                $param['holiday_areas'] = implode(',', $param['holiday_areas']);
            }

            if($param['unholiday_area']) {
                $param['unholiday_area'] = implode(',', $param['unholiday_area']);
            }

            if($param['unholiday_areas']) {
                $param['unholiday_areas'] = implode(',', $param['unholiday_areas']);
            }
            $param['photo_images'] = implode(',',$param['photo_images']);
            $param['tags'] = implode(',',array_filter($param['tags']));

            $teacher_exp = $param['teacher_exp'];
            $_teacher_exp = [];
            foreach ($teacher_exp as $k=>$v){
                if($v['start'] && $v['end'] && $v['content']){
                    $_teacher_exp[] = $v;
                }
            }
            $param['teacher_exp'] = json_encode($_teacher_exp,true);

            $param['honor_images'] = implode(',',$param['honor_images']);
            //不用钱的直接算正常提交了
            if($money){
                $param['status'] = 0;
                $order = (new SettledOrder())->addOrder([
                    'total_fee' => $money,
                    'nums' => $settled_type_count,
                    'settled_type' => $param['settled_type']
                ]);
            }else{
                $param['status'] = 1;
                $order = '';
            }

            $is_auto_pass = false;
            if($passData) {
                if ($this->compare_data($passData, $param)) {
                    $is_auto_pass = true;
                }
            }

            if($is_auto_pass){
                $param['status'] = 2;
                $res = (new TeacherAudit)->isUpdate($isUpdate)->save($param);

                if(!isset($param['id'])){
                    $param['id'] = (new TeacherAudit())->getLastInsID();
                }

                \think\Hook::listen('teacher_to_pass',$param);
            }else{
                $res = (new TeacherAudit)->isUpdate($isUpdate)->save($param);
            }

            Db::commit();
        }catch (\Exception $e){
            Log::record($this->request->param(),'save_apply_orginal_log');
            Log::record($param,'save_apply_save_log');
            Log::record($e->getMessage(),'save_apply_save_msg');

            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('申请成功',['money'=>$money,'order'=>$order,'isUpdate'=>$res,'is_auto_pass'=>$is_auto_pass]);

    }

    //比较之前和当前的数据，判断是否要自动通过
    private function compare_data($before,$after)
    {
        //这些字段不进行比较，其他的字段如果和之前的全部一样，就自动更新
        $auto_pass_field = [
            'status',
            'is_fill',
            'give_time',
            'give_lessons',
            'workaddr_area',
            'workaddr_areas',
        ];
        foreach ($after as $field=>$value){
            if(isset($after[$field]) && isset($before[$field]) && !($after[$field]==$before[$field])){
                if(!in_array($field,$auto_pass_field)){
                    return false;
                }
            }
        }

        //上述代码中会出现即时数据全部没有修改，也能正常提交的情况
        //所以我们把免审核的字段进行比较，如果还是全部相同，就提示更新资料
        $auto_pass_field = [
            'give_time',
            'give_lessons',
            'workaddr_area',
            'workaddr_areas',
        ];
        //检测数据是否修改
        $all_eq = true;
        foreach ($auto_pass_field as $field){
            if(isset($after[$field]) && isset($before[$field]) &&  $after[$field]!==$before[$field]){
                $all_eq = false;
            }
        }
        if($all_eq){
            throw new \Exception('请先更新资料');
        }

        return true;
    }

    public function refund_msg()
    {
        $user_id = $this->auth->id;
        $lists = Db::name('teacher_audit')->where([
            'user_id' => $user_id,
            'status' => 3,
        ])->order('id desc')->field('id,refund_reson,refund_read,refund_time,createtime')->paginate();

        Db::name('teacher_audit')->where([
            'user_id' => $user_id,
            'status' => 3,
            'refund_read' => 0
        ])->update(['refund_read' => 1]);

        $this->success('',$lists);
    }

    //学校列表
    public function school()
    {
        $param = $this->request->param();
        $SchoolModel = new SchoolModel();
        $where = [];
        if(isset($param['keyword']) && $param['keyword']){
            $where['name'] = ['like','%'.$param['keyword'].'%'];
        }
        $lists = $SchoolModel->where($where)->paginate(30);
        $this->success('',$lists);
    }

    //帮您选师
    public function choose()
    {
        $audit_on = ThinkConfig::get('site.audit_on',0);
        if($audit_on==1){
            $this->success('提交成功');
        }
        $param = $this->request->param();

        $param['user_id'] = $this->auth->id;

        $validate = new TeacherChooseValidate();

        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }

        $param['subject'] = (new Subject)->where([
            'id' => $param['subject']
        ])->value('name');

        $param['grade'] = (new Grade())->where([
            'id' => $param['grade']
        ])->value('name');

        $param['addr'] = implode(',',$param['addr']);

        $res = (new TeacherChoose)->save($param);
        if($res){
            $this->success('申请成功');
        }else{
            $this->error('申请失败');
        }
    }

    //下单
    public function create_order(){
        $param = $this->request->param();

        $user_id = $this->auth->id;

        if($user_id==$param['teacher_id']){
            $this->error('不能自己给自己上课','self');
        }
        $student_info = UserModel::where([
            'id' => $user_id,
        ])->field('grade,subject')->find();
        if(!$student_info['grade'] || !$student_info['subject']){
            $this->error('请先前往个人中心完善学生信息','student_info_need_full');
        }
        $teacher = TeacherModel::where([
            'user_id' => $param['teacher_id'],
        ])->find();

//        $is_school = UserModel::where([
//            'id' => $user_id,
//        ])->value('is_school');
//        if(!$is_school){
//            $userCoupons = UserCoupons::where([
//                'user_id' => $user_id,
//            ])->find();
//            if(!$userCoupons || $userCoupons['nums']<=0 ){
//                $this->error('请先购买信息券','to_buy_coupons');
//            }
//        }

        Db::startTrans();

        try{

            //计算首次上课时间
            $weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            $start_time = explode('~', $param['per_week_time'])[0];
            $first_teacher_pos = strtotime('this '.$weekdays[$param['per_week']].' '.$start_time);
            $first_teacher = date('Y-m-d H:i:s', $first_teacher_pos);


            $give_time = json_decode($teacher['give_time'],true);
            $give_time[$param['per_week']][$param['per_timesec']] = '';
            $give_time = json_encode($give_time,true);
            $teacher->give_time = $give_time;
            $teacher->save();

            $grade_info = Grade::where(['id'=>$student_info['grade']])->find();
            $age_range_id = $grade_info['age_range_id'];
            $grade_name = Grade::where(['id'=>$student_info['grade']])->value('name');
            $subject_name = Subject::where(['id'=>$student_info['subject']])->value('name');

            $advise_price = Db::name('age_range')->where([
                'id' => $age_range_id
            ])->value('advise_price');

            $order_id = (new TeacherOrder())->insertGetId([
                'user_id' => $user_id,
                'subject_id' => $student_info['subject'],
                'subject_name' => $subject_name,
                'grade_id' => $student_info['grade'],
                'grade_name' => $grade_name,
                'teacher_id' => $teacher['user_id'],
                'first_teacher' => $first_teacher,
                'per_week' => $weekdays[$param['per_week']],
                'per_week_timesec' => $param['per_timesec'],
                'per_week_time' => $param['per_week_time'],
                'order_sn' => createOrderNo(),
                'status' => 1,
                'money' => $advise_price,
                'createtime' => time()
            ]);


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('报名成功',$order_id);


    }

    //入驻的时候同意我的合同
    public function agreement()
    {
        $user_id = $this->auth->id;
        Db::name('user')->where('id', $user_id)->update(['agreement' => 1]);
        $this->success('');
    }

    //往期资料
    public function before()
    {
        $user_id = $this->auth->id;

        $passData = (new TeacherAudit)->where([
            'user_id' => $user_id,
            'status' => 2
        ])->order('id desc')->find();

        //是否显示教师资格证的提交入口
        $show_main_image = true;
        if($passData && $passData['main_image']){
            $show_main_image = false;
        }

        if($passData){
            $passData['give_time'] = Db::name('teacher')->where([
                'user_id' => $user_id
            ])->value('give_time');
        }

        // status 0 草稿  1 已提交未审核  2 审核成功   3 被驳回
        $unpassData = (new TeacherAudit)->where([
            'user_id' => $user_id,
            'status' => ['in','0,1,3']
        ])->order('id desc')->find();

        if($passData && $unpassData){
            if($unpassData['status'] == 3){
                $data = $passData;
            }else{
                $data = $unpassData;
            }
        }else if($passData && !$unpassData){
            $data = $passData;
        }else if(!$passData && $unpassData){
            $data = $unpassData;
        }else{
            $data = false;
        }

        if($data){
            $data['show_main_image'] = $show_main_image;
            if($data['holiday_pick']){
                $arr = explode(' ', $data['holiday_pick']);
                $province_id = Db::name('xykeep_area')->where(['area_name'=>$arr[0],'parent_id'=>0])->value('area_id');
                $city_id = Db::name('xykeep_area')->where(['area_name'=>$arr[1],'parent_id'=>$province_id])->value('area_id');
                $area_arr = Db::name('xykeep_area')->where(['deep'=>3,'parent_id'=>$city_id])->column('area_name');
//                array_unshift($area_arr,'全市');
                $data['holiday_pick_arr'] =  $area_arr;

                if($data['holiday_areas']){
                    $data['holiday_pick_arr_selected'] = explode(',', $data['holiday_areas']);
                }
            }else{
                $data['holiday_pick_arr'] = [];
                $data['holiday_pick_arr_selected'] = [];
            }
            if($data['unholiday_pick']){
                $arr = explode(' ', $data['unholiday_pick']);
                $province_id = Db::name('xykeep_area')->where(['area_name'=>$arr[0],'parent_id'=>0])->value('area_id');
                $city_id = Db::name('xykeep_area')->where(['area_name'=>$arr[1],'parent_id'=>$province_id])->value('area_id');
                $un_area_arr = Db::name('xykeep_area')->where(['deep'=>3,'parent_id'=>$city_id])->column('area_name');

//                array_unshift($un_area_arr,'全市');
                $data['unholiday_pick_arr'] = $un_area_arr;

                if($data['unholiday_areas']){
                    $data['unholiday_pick_arr_selected'] = explode(',', $data['unholiday_areas']);
                }
            }else{
                $data['unholiday_pick_arr'] = [];
                $data['unholiday_pick_arr_selected'] = [];
            }
        }



        $this->success('往期资料',$data);
    }

    //通过省市获得其下的所有区域
    public function getAreas()
    {
        $data = $this->request->param();

        $province = $data['province'];
        $city = $data['city'];

        $province_id = Db::name('xykeep_area')->where(['area_name'=>$province,'parent_id'=>0])->value('area_id');
        $city_id = Db::name('xykeep_area')->where(['area_name'=>$city,'parent_id'=>$province_id])->value('area_id');
        $area_arr = Db::name('xykeep_area')->where(['deep'=>3,'parent_id'=>$city_id])->column('area_name');
//                array_unshift($area_arr,'全市');
        $this->success('',$area_arr);
    }


    //订单详情
    public function teacher_order_detail(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $detail = TeacherOrder::where([
            'id' => $param['id'],
        ])->find();

        if(!$detail || ($detail['user_id'] !== $user_id && $detail['teacher_id'] !== $user_id)){
            $this->error('订单不存在');
        }

        $student = UserModel::where(['id'=>$detail['user_id']])->find();
        $teacher = UserModel::where(['id'=>$detail['teacher_id']])->find();
        $teacherAudit = TeacherAudit::where([
            'user_id' => $detail['teacher_id'],
        ])->find();

        $detail['teacher'] = $teacher['username'];
        $detail['teacher_mobile'] = $teacher['mobile'];
        $detail['teacher_avatar'] = $teacher['avatar'];
        $detail['teacher_sex'] = $teacher['sex'];
        $detail['realname'] = $teacherAudit['realname'];
        $detail['addr'] = explode(',',$student['addr'])[0];
        $detail['student'] = $student['username'];
        $detail['student_mobile'] = $student['mobile'];
        $detail['student_avatar'] = $student['avatar'];
        $detail['student_sex'] = $student['sex'];
        $detail['state'] = $student['state'];

        $this->success('success',$detail);
    }


    //订单拒绝已读
    public function refund_read()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $find = TeacherOrder::where([
            'id'=>$param['id'],
            'user_id'=>$user_id
        ])->find();
        if(!$find){
            $this->error('没有找到相关订单');
        }
        $find->refund_read = 1;
        $find->save();
        $this->success('success');
    }

    //收藏
    public function collect(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $teacher_collect = new TeacherCollect();
        $collect = $teacher_collect->where([
            'user_id' => $user_id,
            'teacher_id' => $param['teacher_id'],
        ])->find();
        if(!$collect){
            $teacher_collect->save([
                'user_id' => $user_id,
                'teacher_id' => $param['teacher_id'],
            ]);
            $this->success('success',true);
        }else{
            $teacher_collect->where([
                'user_id' => $user_id,
                'teacher_id' => $param['teacher_id'],
            ])->delete();
            $this->success('success',false);
        }
    }

    //我的老师
    public function my_teacher()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $teacher_ids = TeacherOrder::where([
            'user_id' => $user_id,
            'status' => ['in','2,3']
        ])->column('teacher_id');
        $data = TeacherModel::getLists([
            'page' => $param['page'],
            'where' => [
                'user_id' => ['in', $teacher_ids]
            ]
        ]);
        foreach ($data as &$val) {
            $val['mobile'] = UserModel::where([
                'id' => $val['user_id']
            ])->value('mobile');
        }

        $this->success('查询成功',$data);
    }

    //我的学生
    public function my_student()
    {
        $user_id = $this->auth->id;
        $student_ids = TeacherOrder::where([
            'teacher_id' => $user_id,
            'status' => ['in','2,3']
        ])->column('user_id');
        $data = UserModel::where([
            'id' => ['in', $student_ids]
        ])->paginate();
        foreach ($data as $val) {
            $addrList = explode(',', $val['addr']);
            $val['addr'] = $addrList[0].$addrList[1];
        }
        $this->success('查询成功',$data);
    }

    //教师注销
    public function teacher_sign_out()
    {
        $user_id = $this->auth->id;

        TeacherModel::where([
            'user_id' => $user_id
        ])->update([
            'deletetime' => time()
        ]);

        TeacherAudit::where([
            'user_id' => $user_id
        ])->update([
            'deletetime' => time()
        ]);

        $this->success('注销成功');
    }

}